package com.ewili.x.special.service.data.impl;

import com.ewili.framework.utils.DateUtils;
import com.ewili.x.special.dao.inter.StockDao;
import com.ewili.x.special.entity.Stock;
import com.ewili.x.special.service.data.inter.*;
import com.ewili.x.special.task.thread.inter.AnalysisThreadTask;
import com.ewili.x.special.task.thread.inter.ExecsThreadTask;
import com.ewili.x.special.task.thread.inter.ValueThreadTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * @author wonder
 */
@Slf4j
@Service
public class AnalysisServiceImpl implements AnalysisService {

    @Autowired
    private StockDao stockDao;

    @Autowired
    private AnalysisThreadTask analysisThreadTask;

    @Autowired
    private ExecsThreadTask execsThreadTask;

    @Autowired
    private ResearchReportService researchReportService;

    @Autowired
    private GoodWillService goodWillService;

    @Autowired
    private AmountStockService amountStockService;

    @Autowired
    private ValueThreadTask valueThreadTask;

    @Autowired
    private HyRankService hyRankService;


    @Override
    public void analysis() {
        this.reSpideNewestData();
        List<Stock> stocks = stockDao.findAll();
        for (Stock stock : stocks) {
            String code = stock.getCode();
            analysisThreadTask.analysis(code, DateUtils.parse("2020-03-11", DateUtils.yyyy_MM_dd));
        }
    }

    /**
     * 重新抓取最新数据
     */
    private void reSpideNewestData() {
        //高管增减持
        String execsUrl = "http://datainterface.eastmoney.com/EM_DataCenter/JS.aspx?type=GG&sty=GGMX&p=0&ps=100&js=var%20VoSqMrpn={pages:(pc),data:[(x)]}";
        execsThreadTask.spideExecs(execsUrl);

        //分析师报告
        String researchReportUrl = "http://datainterface.eastmoney.com//EM_DataCenter/js.aspx?type=SR&sty=GGSR&js=var%20hklnNyFS={%22data%22:[(x)],%22pages%22:%22(pc)%22,%22update%22:%22(ud)%22,%22count%22:%22(count)%22}&ps=100&p=1&mkt=1&stat=0&cmd=4&code=&rt=51230904";
        try {
            researchReportService.spideResearchReport(researchReportUrl);
        } catch (IOException e) {
            log.error("error");
        }

        //资金
        amountStockService.spide();

        //估值
        List<Stock> stocks = stockDao.findAll();
        for (Stock stock : stocks) {
            String valueUrl = "http://dcfm.eastmoney.com/EM_MutiSvcExpandInterface/api/js/get?type=GZFX_GGZB&token=894050c76af8597a853f5b408b759f5d&st=TRADEDATE&sr=-1&p=1&ps=100&js=var%20BVjHgvpO={pages:(tp),data:(x),font:(font)}&filter=(SECURITYCODE='" + stock.getCode() + "')&rt=52100197";
            valueThreadTask.spideValue(valueUrl, stock.getCode());
        }

        //hyRankService.spideData();

        //大宗交易
        goodWillService.spideGoodWill();
    }
}
